<template>
  <vol-box :lazy="true" v-model="modelOpen" title="选择商品" :height="400" :width="1200" :padding="5"
    :onModelClose="onModelClose">
    <goodsInventoryWarehouse_xm ref="goodsInventoryWarehouse_xm"></goodsInventoryWarehouse_xm>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect">确定</el-button>
      </div>
    </template>
  </vol-box>
</template>

<script>
import VolBox from "@/components/basic/VolBox.vue";
import goodsInventoryWarehouse_xm from "@/views/inventoryinfo/goods_inventory_warehouse_xm/q_goods_inventory_warehouse_xm.vue";
export default {
  components: {
    "vol-box": VolBox,
    goodsInventoryWarehouse_xm,
  },
  data() {
    return {
      modelOpen: false,
      // form: {
      //   goods_sub: "",
      //   goods_name: "",
      // },
      // tableList: [],
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.goodsInventoryWarehouse_xm.searchFormFields.库位 = null;
      this.$refs.goodsInventoryWarehouse_xm.searchFormFields.编码 = null;
      this.$refs.goodsInventoryWarehouse_xm.searchFormFields.名称 = null;
      this.$refs.goodsInventoryWarehouse_xm.searchFormFields.品牌 = null;
      this.$refs.goodsInventoryWarehouse_xm.searchFormFields.最后入库时间 =
        null;
      this.$refs.goodsInventoryWarehouse_xm.searchFormFields.可用数量 = [
        0.1,
        null,
      ];
      // console.log(" this.$refs.goodsInventoryWarehouse_xm", this.$refs.goodsInventoryWarehouse_xm.$refs.grid.$refs.table.reset);
      this.$refs.goodsInventoryWarehouse_xm.$refs.grid.$refs.table.reset()
      // await this.$refs.goodsInventoryWarehouse_xm.$refs.grid.search();
    },
    open(Goods_sub, WHSE_jg) {
      this.Tax_rate = null;


      this.modelOpen = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.goodsInventoryWarehouse_xm) {
          clearInterval(setIntervalTiem);
          if (this.$route.path == "/q_serve_contract" && Goods_sub) {
            this.Tax_rate = Goods_sub;
            this.$refs.goodsInventoryWarehouse_xm.searchFormFields.库位 = WHSE_jg
            this.$refs.goodsInventoryWarehouse_xm.searchFormOptions.forEach(
              (item) => {
                item.forEach((x) => {
                  // if (x.field == "上级库位") x.disabled = true;
                  if (x.field == "库位") x.disabled = true;
                });
              })
          }
          if ((this.$route.path == "/q_produce_use" || this.$route.path == "/q_produce_return") && Goods_sub) {
            this.WHSE_scgw = Goods_sub;
            this.$refs.goodsInventoryWarehouse_xm.searchFormFields.库位 = WHSE_jg
          }
          this.$refs.goodsInventoryWarehouse_xm.searchFormFields.可用数量 = [
            0.1,
            null,
          ];
          if (
            (this.$route.path == "/q_stock_out" ||
              this.$route.path == "/q_damage_voucher" ||
              this.$route.path == "/q_warehouse_allot" ||
              this.$route.path == "/q_warehouse_add_allot" ||
              this.$route.path == "/q_fittings_delivery" ||
              this.$route.path == "/q_sales_contract" ||
              this.$route.path == "/q_sales" ||
              this.$route.path == "/q_after_delivery") &&
            Goods_sub
          ) {
            console.log("Goods_sub", Goods_sub);
            // this.$refs.goodsInventoryWarehouse_xm.searchFormFields.上级库位 = [
            //   Goods_sub,
            // ];//级联的时候使用
            this.$refs.goodsInventoryWarehouse_xm.searchFormFields.库位 = Goods_sub;//select的时候使用
            this.$refs.goodsInventoryWarehouse_xm.searchFormOptions.forEach(
              (item) => {
                item.forEach((x) => {
                  // if (x.field == "上级库位") x.disabled = true;
                  if (x.field == "库位") x.disabled = true;
                });
              }
            );
          }
          if (this.$route.path == "/q_warehouse_add_allot") {
            this.Ja_ja_lv = WHSE_jg ? WHSE_jg / 100 : 0
            // console.log("Ja_ja_lv",this.Ja_ja_lv);
          }
          this.$refs.goodsInventoryWarehouse_xm.$refs.grid.search();
        }
      }, 100);
    },
    async onSelect() {
      let rowObj =
        this.$refs.goodsInventoryWarehouse_xm.$refs.grid.getSelectRows();
      if (!rowObj.length)
        return this.base.ElMessageFn("请至少选择一件商品", "warning");
      if (this.$route.path == "/q_sales_contract" || this.$route.path == "/q_sales") {
        this.$emit("parentCall", ($parent) => {
          try {
            //处理明细表已含有相同数据
            let parentList = $parent.$refs.detail.rowData;
            let set = new Set(
              parentList.map((item) => item.goodsInventoryWarehouseXm)
            ); //得到由明细表goods_sub组成的数组
            let commonObjects = rowObj.filter((item) => set.has(item.ID)); //得到明细表中已包含的选中行
            let goodsNamesSet = new Set(commonObjects.map((item) => item.ID)); //相同商品的编码
            if (commonObjects.length) {
              rowObj = rowObj.filter((item) => !goodsNamesSet.has(item.ID)); //在选中数据中剔除明细表已包含的行数据
              let str = "";
              commonObjects.forEach((r) => {
                if (str == "") str += r.名称;
                else str = str + "," + r.名称;
              });
              this.$message.warning(`商品${str}已存在此明细表`);
            }
            // console.log("$parent.editFormFields.Tax_rate",$parent.editFormFields.Tax_rate)
            let arr;
            if (this.$route.path == "/q_sales_contract") {
              arr = rowObj.map((item) => {
                return {
                  goodsInventoryWarehouseXm: item.ID,
                  Goods_sub: item.编码,
                  Goods_name: item.名称,
                  Goods_size: item.规格,
                  // WHSE_name: item.库位,
                  // AvailableQuantity: item.可用数量,
                  CostPrice: item.成本价,
                  Logistics_status: 0,
                  Install_status: 0,
                  Goods_num: 1,
                  Goods_price: item.成本价,
                  Total_price: item.成本价,
                  Tax_exclusive_goods_price: $parent.editFormFields.Tax_rate ? this.base.roundToFourDecimals(item.成本价 / (1 + +$parent.editFormFields.Tax_rate)) : item.成本价,
                  Tax_exclusive_total_price: $parent.editFormFields.Tax_rate ? this.base.roundToFourDecimals(item.成本价 / (1 + +$parent.editFormFields.Tax_rate)) : item.成本价,
                };
              });
            }
            if (this.$route.path == "/q_sales") {
              arr = rowObj.map((item) => {
                return {
                  Inventory_ID: item.ID,
                  WHSE_name: item.库位,
                  Goods_sub: item.编码,
                  Goods_name: item.名称,
                  Goods_size: item.规格,
                  // WHSE_name: item.库位,
                  AvailableQuantity: item.可用数量,
                  Goods_num: 1,
                  Goods_price: item.成本价,
                  Total_price: item.成本价,
                  Tax_exclusive_contract_total_price: $parent.editFormFields.Tax_rate ? this.base.roundToFourDecimals(item.成本价 / (1 + +$parent.editFormFields.Tax_rate)) : item.成本价,
                  Cost_price: item.成本价,
                  Cost_price_total_price: item.成本价,
                  Tax_exclusive: item.无税成本,
                  Tax_exclusive_total_price: item.无税成本,

                };
              });
            }

            $parent.$refs.detail.rowData.unshift(...arr);
            if (arr.length) {
              $parent.WHSE_IdDisableFn();
            }
          } catch (error) {
            console.log(error);
          }
        });
        this.onModelClose();
        return (this.modelOpen = false);
      }
      let off = rowObj.some((item) => item.可用数量 <= 0);
      if (off) {
        return this.$message.error("请勿选择无可用数量商品");
      }
      if (this.$route.path == "/q_stock_out" || this.$route.path == "/q_fittings_delivery" || this.$route.path == "/q_after_delivery") {
        let list = await rowObj.filter((item) => item.可用数量 > 0); //有库存
        let list2 = await rowObj.filter((item) => item.可用数量 <= 0); //无库存
        if (list2.length) {
          let str = "";
          list2.forEach((r) => {
            if (str == "") str += r.名称;
            else str = str + "," + r.名称;
          });
          this.$message.warning(`商品${str}无可用库存`);
        }
        this.$emit("parentCall", ($parent) => {
          try {
            //处理明细表已含有相同数据
            let parentList = $parent.$refs.detail.rowData;
            let set = new Set(
              parentList.map((item) => item.goodsInventoryWarehouseXm)
            ); //得到由明细表goods_sub组成的数组
            let commonObjects = list.filter((item) => set.has(item.ID)); //得到明细表中已包含的选中行
            let goodsNamesSet = new Set(commonObjects.map((item) => item.ID)); //相同商品的编码
            if (commonObjects.length) {
              list = list.filter((item) => !goodsNamesSet.has(item.ID)); //在选中数据中剔除明细表已包含的行数据
              let str = "";
              commonObjects.forEach((r) => {
                if (str == "") str += r.名称;
                else str = str + "," + r.名称;
              });
              this.$message.warning(`商品${str}已存在此明细表`);
            }
            let arr;
            if (this.$route.path == "/q_fittings_delivery" || this.$route.path == "/q_after_delivery") {
              arr = list.map((item) => {
                return {
                  goodsInventoryWarehouseXm: item.ID,
                  Goods_sub: item.编码,
                  Goods_name: item.名称,
                  Goods_size: item.规格,
                  WHSE_name: item.库位,
                  AvailableQuantity: item.可用数量,
                  Goods_price: item.成本价,
                  CostPrice: item.成本价,
                  Tax_exclusive: item.无税成本,
                  Recycle: "0",
                };
              });
            }
            if (this.$route.path == "/q_stock_out") {
              arr = list.map((item) => {
                return {
                  goodsInventoryWarehouseXm: item.ID,
                  Goods_sub: item.编码,
                  Goods_name: item.名称,
                  Goods_size: item.规格,
                  WHSE_name: item.库位,
                  AvailableQuantity: item.可用数量,
                  CostPrice: item.成本价,
                };
              });
            }

            $parent.$refs.detail.rowData.unshift(...arr);
            if (arr.length) {
              $parent.WHSE_IdDisableFn();
            }
          } catch (error) {
            console.log(error);
          }
        });
        this.onModelClose();
        return (this.modelOpen = false);
      }

      if (this.$route.path == "/q_serve_contract") {
        this.$emit("parentCall", ($parent) => {
          let detailDate = $parent.$refs.detail.rowData;

          let arrID = [];
          detailDate.forEach((item) => {
            let index = rowObj.findIndex((x) => {
              x.ID == item.goodsInventoryWarehouseXm
                ? arrID.push({ ID: x.ID, goods_name: x.名称 })
                : "";
              return x.ID == item.goodsInventoryWarehouseXm;
            });
            if (index != -1) {
              rowObj.splice(index, 1);
            }
          });
          let strTitle = "";
          arrID.forEach((item) => {
            if (strTitle != "") {
              strTitle += "," + item.goods_name;
            } else {
              strTitle = item.goods_name;
            }
          });
          if (strTitle != "") {
            this.base.ElMessageFn("商品" + strTitle + "已存在安装单明细");
          }
          let newRowObj = [];
          if (this.Tax_rate) {
            // console.log("this.Tax_rate", this.Tax_rate);
            const Tax_rate = 1 + +this.Tax_rate;
            newRowObj = rowObj.map((item) => {
              return {
                goodsInventoryWarehouseXm: item.ID,
                Goods_sub: item.编码,
                Goods_name: item.名称,
                Goods_num: 1,
                Goods_size: item.规格,
                Total_price: item.建议售价,
                Goods_price: item.建议售价,
                Cost_price: item.成本价,
                WHSE_name: item.库位名称,
                Tax_exclusive_goods_price: item.建议售价
                  ? this.base.roundToFourDecimals(item.建议售价 / Tax_rate)
                  : "",
                Tax_exclusive_total_price: item.建议售价
                  ? this.base.roundToFourDecimals(item.建议售价 / Tax_rate)
                  : "",
              };
            });
          } else {
            newRowObj = rowObj.map((item) => {
              return {
                goodsInventoryWarehouseXm: item.ID,
                Goods_sub: item.编码,
                Goods_name: item.名称,
                Goods_num: 1,
                Goods_size: item.规格,
                Total_price: item.建议售价,
                Goods_price: item.建议售价,
                Cost_price: item.成本价,
              };
            });
          }

          $parent.$refs.detail.rowData.unshift(...newRowObj);
          this.onModelClose();
        });
        return (this.modelOpen = false);
      }

      if (this.$route.path == "/q_produce_return") {
        this.$emit("parentCall", ($parent) => {
          let detailRowData = $parent.$refs.detail.rowData;
          let newRowObj = [];
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              Goods_num: 1,
              Goods_size: item.规格,
              WHSE_name: item.库位,
              Production_state: 1,
              AvailableQuantity: item.可用数量,
              Cost_price: item.成本价,
              Tax_exclusive: item.无税成本,
              Type: 1,
              WHSE_no: this.WHSE_scgw,
              Cost_accounting: item.成本核算,
            };
          });
          let arr = [];
          let stringName = "";
          detailRowData.forEach((item) => {
            newRowObj.forEach((j) => {
              if (
                j.goodsInventoryWarehouseXm == item.goodsInventoryWarehouseXm &&
                item.Type == 1
              ) {
                stringName += stringName ? "," + j.Goods_name : j.Goods_name;
                arr.push(j.goodsInventoryWarehouseXm);
              }
            });
          });
          if (stringName != "") {
            console.log(stringName);
            this.$message.warning("明细表已存在 " + stringName);
          }
          arr.forEach((item) => {
            let index = newRowObj.findIndex(
              (x) => x.goodsInventoryWarehouseXm == item
            );
            if (index != -1) {
              newRowObj.splice(index, 1);
            }
          });
          $parent.$refs.detail.rowData.unshift(...newRowObj);
          this.onModelClose();
        });
        return (this.modelOpen = false);
      }
      this.$emit("parentCall", ($parent) => {
        let parentList = $parent.$refs.detail.rowData;
        let set = new Set(
          parentList.map((item) => item.goodsInventoryWarehouseXm)
        ); //得到由明细表goods_sub组成的数组
        let commonObjects = rowObj.filter((item) => set.has(item.ID)); //得到明细表中已包含的选中行
        let goodsNamesSet = new Set(commonObjects.map((item) => item.ID)); //相同商品的编码
        if (commonObjects.length) {
          rowObj = rowObj.filter((item) => !goodsNamesSet.has(item.ID)); //在选中数据中剔除明细表已包含的行数据
          let str = "";
          commonObjects.forEach((r) => {
            if (str == "") str += r.名称;
            else str = str + "," + r.名称;
          });
          this.$message.warning(`商品${str}已存在此明细单`);
        }
        let newRowObj = [];

        if (this.$route.path == "/q_warehouse_allot") {
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              CostPrice: item.成本价,
              Goods_size: item.规格,
              WHSE_name_out: item.库位, //库位名称
              AvailableQuantity: item.可用数量,
              Cost_accounting: item.成本核算,
              // Tax_exclusive: item.无税成本,
            };
          });
        } else if (
          this.$route.path == "/q_warehouse_add_allot"
        ) {
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              CostPrice: item.成本价,
              Goods_size: item.规格,
              WHSE_name_out: item.库位, //库位名称
              AvailableQuantity: item.可用数量,
              Cost_accounting: item.成本核算,
              Tax_exclusive: item.无税成本,
              Goods_price: this.base.roundToFourDecimals(item.成本价 * (1 + this.Ja_ja_lv))
            };
          });
        } else if (
          this.$route.path == "/q_other_in_out"
        ) {
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              Cost_price: item.成本价,
              Goods_size: item.规格,
              WHSE_name: item.库位,
              AvailableQuantity: item.可用数量,
              Cost_accounting: item.成本核算,
              Tax_exclusive: item.无税成本,
              Type: "报损",
            };
          });
        } else if (
          this.$route.path == "/q_damage_voucher"
        ) {
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              CostPrice: item.成本价 || 0,
              Goods_size: item.规格,
              WHSE_name: item.库位,
              AvailableQuantity: item.可用数量,
              Cost_accounting: item.成本核算,
              Tax_exclusive: item.无税成本 || 0,
              // Type: "报损",
            };
          });
        } else if (this.$route.path == "/q_produce_use") {
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              Goods_size: item.规格,
              WHSE_name: item.库位,
              AvailableQuantity: item.可用数量,
              Goods_num: 1,
              Cost_price: item.成本价,
              Tax_exclusive: item.无税成本,
              WHSE_no: this.WHSE_scgw,
              Cost_accounting: item.成本核算,
            };
          });
        } else {
          // /q_stock_out
          newRowObj = rowObj.map((item) => {
            return {
              goodsInventoryWarehouseXm: item.ID,
              Goods_sub: item.编码,
              Goods_name: item.名称,
              // CostPrice: item.成本价,
              Goods_size: item.规格,
              WHSE_name: item.库位,
              AvailableQuantity: item.可用数量,
              // Tax_exclusive: item.无税成本,
            };
          });
        }
        if (newRowObj.length && (this.$route.path == "/q_damage_voucher" || this.$route.path == "/q_warehouse_allot" || this.$route.path == "/q_warehouse_add_allot")) {
          $parent.WHSE_IdDisableFn();
        }
        $parent.$refs.detail.rowData.unshift(...newRowObj);
        this.onModelClose();
        return (this.modelOpen = false);
      });
    },
  },
};
</script>

<style scoped></style>